package com.freewater.javabase.demo.nio;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class ScatterRead {
    public static void main(String[] args) throws IOException {
        RandomAccessFile file = new RandomAccessFile("data/nio-data.txt", "rw");
        final FileChannel channel = file.getChannel();
        ByteBuffer buffer1 = ByteBuffer.allocate(3);
        ByteBuffer buffer2 = ByteBuffer.allocate(8);

        ByteBuffer[] buffers = new ByteBuffer[]{buffer1, buffer2};
        channel.read(buffers);

        buffer1.flip();
        while (buffer1.hasRemaining()) {
            System.out.println((char) buffer1.get());
        }

        System.out.println("----------------------");

        buffer2.flip();
        while (buffer2.hasRemaining()) {
            System.out.println((char) buffer2.get());
        }

        buffer1.clear();
        buffer2.clear();

        channel.close();
    }
}
